<template>

    <div id="job-approval" class="w-h-100 pos-r flex-1 apply-container page-public-s" style="box-sizing: border-box;overflow:hidden;">
        <div class="w-h-100 ver-flex pos-a" style="box-sizing: border-box; padding: 6px;">
            <div style="box-sizing: border-box;">
                <el-card shadow="never" :body-style="{ paddingBottom: '0' }">
                    <el-form class="box-f" ref="queryForm" :inline="true">
                        <el-form-item :label="$t('b0t63v7bGsmbXkQdwW4Q0')">
                            <el-select 
                                multiple
                                collapse-tags
                                v-model="searchVal" 
                                class="m-2" 
                                :placeholder="$t('g6AmUBb2H8qwj8DuC0TH')" 
                                @change="searchChange"
                            >
                                <el-option
                                    v-for="item in searchOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <div class="search-f">
                                <template v-for="(item, index) in searchVal">
                                    <!-- 审批状态 -->
                                    <template v-if="item === '1'">
                                        <div style="padding: 0 10px">
                                            <el-select 
                                                v-model="stateVal" 
                                                filterable 
                                                class="m-2" 
                                                :placeholder="$t('uk_220Omr0p3DgKxFxwB4')" 
                                                @change="acquireChange"
                                                value-key="id"
                                            >
                                                <el-option
                                                    v-for="item in auditPle"
                                                    :key="item.value"
                                                    :label="item.label"
                                                    :value="item.value"
                                                />
                                            </el-select>
                                        </div>
                                    </template>
                                    <!-- 模糊查询 -->
                                    <template v-if="item === '2'">
                                        <div style="padding: 0 10px">
                                            <el-input style="width: 200px;" v-model="blurVal" :placeholder="$t('yeyw245R7a6VYy19Otp1n')" />
                                        </div>
                                    </template>
                                    <!-- 时间段查询 -->
                                    <template v-if="item === '3'">
                                        <div style="padding: 0 10px">
                                            <el-date-picker
                                                v-model="timeVal"
                                                type="datetimerange"
                                                range-separator="-"
                                                :start-placeholder="$t('zxGaloajaAo1yMttcStime')"
                                                :end-placeholder="$t('zxGaloajaAo1yMttcEtime')"
                                                value-format="YYYY-MM-DD HH:mm:ss"
                                            />
                                        </div>
                                    </template>
                                </template>
                            </div>
                        </el-form-item>
                        <el-form-item >
                            <el-button @click='searchClick' type="primary" icon="ele-Search"> {{ $t('a4Uw6qeAZpctNhl3u7Uge') }} </el-button>
                            <el-button @click="resetClick" icon="ele-Refresh"> {{ $t('rxCkXeM2sO75ltcf8JyVj') }} </el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>

            <div style="box-sizing: border-box;" class="flex-1 pos-r">
                <div class="pos-a w-h-100" style="box-sizing: border-box;">
                    <el-card class="box-card w-h-100" shadow="never" style="margin-top: 16px;">
                        <div style="" class="w-h-100 ver-flex">
                            <div class="flex-1 pos-r">
                                <div class="pos-a w-h-100">

                                    <el-table :data="list" class="w-h-100" style="width: 100%" border v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.5)">
                                        
                                        <el-table-column type="index" :label="$t('3eKEs1bExAXarYpdJemp')" width="55" align="center" :index="indexMethod" />
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('7YrNDepF6YGnXofIKj30l')"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('7YrNDepF6YGnXofIKj30l')" v-model="searchVar.v_ApplyNO" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.jobApply. v_ApplyNO">{{ scope.row.jobApply.v_ApplyNO }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('tvGbL1MehJrp5EtEfWmB')"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('tvGbL1MehJrp5EtEfWmB')" v-model="searchVar.v_ProjectName" />
                                                </template>
                                                <template #default="scope">
                                                    <template v-if="scope.row.n_Type === 1">
                                                        <span v-if="scope.row.jobApply.v_ProjectName === ''">-</span>
                                                        <span v-else>{{ scope.row.jobApply.v_ProjectName }}</span>
                                                    </template>
                                                    <template v-else>
                                                        <span v-if="scope.row.securityCheckChange.v_ProjectName === ''">-</span>
                                                        <span v-else>{{ scope.row.securityCheckChange.v_ProjectName }}</span>
                                                    </template>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('epwS2ejR2ZhyNhHnB2wd')"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('epwS2ejR2ZhyNhHnB2wd')" v-model="searchVar.auditEmpName" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.auditEmpName"> {{ scope.row.auditEmpName }} </span>
                                                    <span v-else>-</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('uk_220Omr0p3DgKxFxwB4')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('uk_220Omr0p3DgKxFxwB4')" v-model="searchVar.n_AuditState" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.n_AuditState === -1" style="color: #F56C6C;">{{ $t('xNx5qj0Ad0Oxydt0q1C') }}</span>
                                                    <span v-else-if="scope.row.n_AuditState === 0">{{ $t('gf0Sq2nS1hOty874oBt4g') }}</span>
                                                    <span v-else-if="scope.row.n_AuditState === 1" style="color: #67C23A;">{{ $t('1uO8o9kmpBShOnoFlN') }}</span>
                                                    <span v-else-if="scope.row.n_AuditState === 2" style="color: #E6A23C;">{{ $t('tzuZnLza7bAuCxmnbIFoc') }}</span>
                                                    <span v-else>{{ $t('u9BFyR2tsG3wjB01C77Rw') }}</span>
                                                    <!-- <span v-else>{{ $t('vDX1cjYx_6HyeG54ajvl') }}</span> -->
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('gtNtFNFs5KVz4bK5fN')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('gtNtFNFs5KVz4bK5fN')" v-model="searchVar.n_Type" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.n_Type === 1">{{ $t('mQgxCgh7OKeOldiMkgFop') }}</span>
                                                    <span v-else>{{ $t('o3G0HGwS4NfeA0MjIsxqi') }}</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column width="260" :formatter="formatDate" :show-overflow-tooltip="true" :label="$t('jfvBMMqrg8Hgmvf1QvEr')"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" width="260" align="center">
                                                <template #header>
                                                    <el-date-picker
                                                        class="set-t"
                                                        v-model="searchVar.d_AuditTime"
                                                        type="datetime"
                                                        value-format="YYYY-MM-DD HH:mm:ss"
                                                        :placeholder="$t('xYKtSx361Hm1x9r3giOx')"
                                                        popper-class="el-date-p"
                                                    >
                                                    </el-date-picker>
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.d_AuditTime"> {{ scope.row.d_AuditTime }} </span>
                                                    <span v-else>-</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('eHdy6iUn3cS1Nqd3eXMnV')" align="center">
                                            <template #default="scope">
                                                <span style="cursor: pointer;" @click="fileClick(scope.row.jobApply.v_JobFilePath)" v-if="scope.row.jobApply.v_JobFilePath && scope.row.jobApply.v_JobFilePath.length !==0">{{ $t('2KyEk7Ir2Lz3bN9ByjSaE') }}</span>
                                                <span v-else>{{ $t('tzv7Fao_8LvfFbddIkvDu') }}</span>
                                            </template>
                                        </el-table-column>
                                        
                                        <el-table-column prop="v_Remake" min-width="160" :show-overflow-tooltip="true" :label="$t('9RLaVEiJf1Z1Qv15o6Kll')" align="center">
                                            <template #default="scoped">
                                                <span v-if="scoped.row.v_Remake">{{ scoped.row.v_Remake }}</span>
                                                <span v-else> - </span>
                                            </template>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" prop="createdTime" :label="$t('xxAj6fsjvIsQrMdlNpolv')" align="center" />
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" prop="createdUserName" :label="$t('v7AhfaNeGaFoPfZzgpFk')" align="center" />

                                        <el-table-column min-width="160" :formatter="formatDate" :show-overflow-tooltip="true" prop="updatedTime" :label="$t('hMMqYalIdWhk0McqdxTg')"  align="center" />

                                        <el-table-column min-width="160" :formatter="formatDate" :show-overflow-tooltip="true" prop="updatedUserName" :label="$t('yRc0RHyyMh9Cy0TKyYr')"  align="center" />

                                        
                                        <el-table-column min-width="180" :label="$t('liBmO8wb58NMw5HLu9Nf')" align="center" fixed="right">
                                            <template #default="scope">
                                                <!-- <el-button @click="nodeClick(scope.row)" class="icon-e" icon="ele-View" size="small" text type="primary"> {{ $t('dqIm8ahxKjSzxhEGtZqjj') }} </el-button> -->
                                                <el-button v-if="$hasPer('jobApproval:detail')" @click="nodeClick(scope.row,'1')" class="icon-e" icon="ele-View" size="small" text type="primary"> {{ $t('ucC9qxHvkvdh9OGEo1pn') }} </el-button>
                                                <el-button v-if="scope.row.n_AuditState === 3 && $hasPer('jobApproval:sp')" @click="nodeClick(scope.row,'2')" class="icon-e" icon="ele-EditPen" size="small" text type="warning"> {{ $t('phVbnlAyS4DkKm4tSGfy') }} </el-button>
                                                <el-button v-else disabled class="icon-e" icon="ele-EditPen" size="small" text type="danger"> {{ $t('tjDQtUi5iIfe7NaQhhVd6') }} </el-button>
                                                <!-- <el-button v-if="scope.row.n_AuditState === 3" @click="deleteHandle(scope.row.n_Id , getData)" class="icon-e" icon="ele-EditPen" size="small" text type="warning"> {{ $t('phVbnlAyS4DkKm4tSGfy') }} </el-button>
                                                <el-button v-else disabled class="icon-e" icon="ele-EditPen" size="small" text type="danger"> {{ $t('tjDQtUi5iIfe7NaQhhVd6') }} </el-button> -->
                                            </template>
                                        </el-table-column>
                                    
                                    </el-table>
                                </div>
                            </div>
                            <div style="height: 30px;margin-top: 20px;" class="hr-flex-start">
								<el-pagination
									v-model:currentPage="pageNo"
									v-model:page-size="pageSize"
									:total="totalRows"
									:page-sizes="[20, 50, 100]"
									small
									background
									@size-change="handleSizeChange"
									@current-change="handleCurrentChange"
									layout="total, sizes, prev, pager, next, jumper"
									class="w-h-100"
								/>
							</div>
                        </div>
                    </el-card>
                </div>
            </div>
        </div>
        <approval-detail
            :currentState="currentState"
            :repairInfo="repairInfo"
            :isUseDialog="isUseDialog"
            @updateData="getData"
            @cancelUseClick="cancelUseClick"
            @controlUseClick="controlUseClick"
        />
        <VModel
            :fileInfo="fileInfo"
            :isModelDialog="isModelDialog"
            @cancelModelClick="cancelModelClick"
        />
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted , reactive , toRaw } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import getTableData from '/@/utils/getTableData';
import ApprovalDetail from './component/approval-detail.vue';
import { JobAuditPage , JobApplyChangeAuditState } from '/@/api/sms/sms';
import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
import VModel from '../components/v-model.vue';

export default defineComponent( {
    name: 'job-approval',
    components: { ApprovalDetail , VModel },
    setup(props){
        const { proxy } = useCurrentInstance();
        const $t = proxy.$t;
        
        const isUseDialog = ref() // 新增/编辑弹出框隐藏/显示
        const repairInfo = ref() // 传递子组件数据
        const currentState = ref(false)

        const isModelDialog = ref(false);
        const cancelModelClick = () => {isModelDialog.value = false};
        const fileInfo = ref('');

        /*** 查看文件 */
        const fileClick = (val: any) => {
            isModelDialog.value = true;
            let setObj: any = {};
            setObj.titleInfo = $t('eHdy6iUn3cS1Nqd3eXMnV');
            setObj.rectifyListArr = val;
            fileInfo.value = JSON.stringify(setObj);
        }

        const searchVar = reactive({
            v_ApplyNO: '',
            v_ProjectName: '',
            auditEmpName: '',
            n_AuditState: '',
            n_Type: '',
            d_AuditTime: '',
        });

        // 搜索类
        const searchVal = ref(['1'])
        const stateVal:any = ref(0) // 审批状态查询
        const blurVal = ref() // 模糊查询
        const timeVal = ref() // 时间段查询
        const searchOptions = ref([{
            value: '1',
            label: $t('uk_220Omr0p3DgKxFxwB4'),
            vals: stateVal,
        }, {
            value: '2',
            label: $t('3dBd6Gp5MqNm4A5dvcqJ4'),
            vals: blurVal,
        }, {
            value: '3',
            label: $t('bxOzlOcGrW1Oanu81ddgi'),
            vals: timeVal,
        }])
        let searchObj:any = reactive({ 
            n_AuditState: 0,
            searchValue: '',
            startTime: '',
            endTime: '',
        })
        /*** 搜索类型选择事件 searchChange */
        const searchChange = (val:any) => {
            if(val.length === 0){
                searchVal.value = ['1']
                stateVal.value = 0// 审批状态查询
                blurVal.value = '' // 模糊查询
                timeVal.value = '' // 时间段查询
            }else{
                let getArr = getArrDifference(val,toRaw(searchOptions.value))
                if(getArr.length !== 0){
                    getArr.map((i:any) => {
                        toRaw(searchOptions.value)[i - 1].vals.value = ''
                    })
                }
            }
            searchVal.value = val
        }
        /*** getArrDifference 提取两个数组中不同的元素 */
        const getArrDifference = (arr1: any | [] , arr2: any | []) => {
            let getArr = []
            for(let z in arr2){
                getArr.push(arr2[z].value)
            }
            return arr1.concat(getArr).filter((v:any, i:any, arr:any) => {
                return arr.indexOf(v) === arr.lastIndexOf(v)
            })
        }
        /*** acquireChange 搜索下拉框选择的数据 ---- 传给接口数据 */
        const acquireChange = (val:any) => {
            console.log(val)
        }
        /*** searchClick 查询按钮 */
        const searchClick = () => {
            if(searchVal.value.length === 0){
                ElMessage({
                    type: 'warning',
                    message: $t('rLGsDpvZSoAzOg47pdRcl'),
                    duration: 1000,
                    onClose: ()=>{
                        resetClick()
                    } 
                })
            }else{
                if(stateVal.value !== undefined && stateVal.value !== ''){
                    searchObj.n_AuditState = stateVal.value
                }
                if(blurVal.value !== undefined && blurVal.value !== ''){
                    searchObj.searchValue = blurVal.value
                }
                if(toRaw(timeVal.value) && toRaw(timeVal.value) !== ''){
                    searchObj.startTime = toRaw(timeVal.value)[0] 
                    searchObj.endTime = toRaw(timeVal.value)[1]
                }else{
                    searchObj.startTime = ''
                    searchObj.endTime = ''
                }
            }
        }
        /*** resetClick 重置按钮 */
        const resetClick = () => {
            searchObj.n_AuditState = 0
            searchObj.searchValue = ''
            searchObj.startTime = ''
            searchObj.endTime = ''

            searchVal.value = ['1']
            stateVal.value = 0
            blurVal.value = '' // 模糊查询
            timeVal.value = '' // 时间段查询
        }
        // 搜索类

        /*** 截取日期格式方法 */
        const formatDate = (row:any, column:any) => {
          // 获取单元格数据
          let data = row[column.property]
          if(data == $t('utiIUzqSzcKy0QiDc49n')){
            return '-'
          }else{
            return data
          }
        }

        /*** nodeClick 新增/编辑点击事件 */
        const nodeClick = (val:string | object , s:String) => {
            val !== '' ? repairInfo.value = JSON.stringify(toRaw(val)) : repairInfo.value = ''
            if(s === '1') currentState.value = false;
            if(s === '2') currentState.value = true;
            isUseDialog.value = true
        }

        /*** deleteHandle 审批数据事件 */
        const deleteHandle = (val: String | Number , updateFun: Function) => {
            let param = {n_Id : val}
            ElMessageBox.alert($t('gZlxg5BiTdVQf1nIHePp'),{
                confirmButtonText: $t('6yDMpz86bcR_0QTpmavPz'),
                cancelButtonText: $t('wFa44E4Iz025Ha2gYeDop'),
            })
            .then(async () => {
                let res:any = await JobApplyChangeAuditState(param)
                if(res.success){
                    updateFun()
                }else{
                    ElMessage({
                        type: 'info',
                        message: res.message,
                    }) 
                }
            })
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            isUseDialog.value = false
        }

        /*** controlUseClick 提交事件 */
        const controlUseClick = async(val:any) => {
            isUseDialog.value = false
        }

        return {
            ...getTableData(JobAuditPage,{},searchObj),
            isUseDialog,
            cancelUseClick,
            controlUseClick,
            nodeClick,
            repairInfo,
            deleteHandle,
            searchVar,

            // 搜索类
            stateVal,
            blurVal,
            timeVal,
            searchVal,
            resetClick,
            searchClick,
            searchChange,
            acquireChange,
            searchOptions, // 搜索方式
            auditPle: [
                { label: $t('phlQhIwNkw1nDaSmhxpG8') , value: 0 },
                { label: $t('w4VhwoPcGmZvsLAw9Ejgt') , value: -1 },
                { label: $t('yGsrk2SvPdeMMz9RtDiLl') , value: 1 },
                { label: $t('1Eky213Jts1mf6p7goA9L') , value: 2 },
                { label: $t('u9BFyR2tsG3wjB01C77Rw') , value: 3 },
                // { label: $t('gf1hWfVmK0UkFtSUxMxtg') , value: 4 },
            ], // 审批状态
            // 搜索类

            formatDate,
            currentState,

            isModelDialog,
            cancelModelClick,
            fileInfo,
            fileClick,
        }
    },
} )
</script>
<style lang="scss"> @import '../style/public.scss'; </style>
<style scoped lang="scss">
    #job-approval {
        .box-f{
            :deep(.el-form-item__label){
                padding: 0;
            }
        }
        :deep(.el-card__body) {
            height: 100%;
        }
        :deep(.el-card){
            border: none !important;
        }
        ul {

            list-style: none;

            li {
                cursor: pointer;

                &:hover {
                    color: red;
                }
            }
        }
        .search-f{
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
        }
        .icon-e{
            :deep(.el-icon){
                margin-right: 0px!important;
            }
        }
    }
</style>